const { updateUserChannel, getUserChannel, addUserChannel, getUserChannelByUserId } = require("../services/UserChannelService");

class ChannelController {

  // 创建用户头像
  async updateUserChannel (ctx) {
    const {userId, channel} = ctx.request.body;
    const count = await getUserChannelByUserId(userId);
    if (count.length) {
      const result = await updateUserChannel(userId,channel)
      if (result) {
        ctx.body = {
          code: 200,
          msg: '更新用户频道成功'
        }
      } else {
        ctx.body = {
          code: 10001,
          msg: '更新用户频道失败'
        }
      }
      return;
    }
    const result = await addUserChannel(userId,channel)
    if (result) {
      ctx.body = {
        code: 200,
        msg: '添加用户频道成功'
      }
    } else {
      ctx.body = {
        code: 10001,
        msg: '添加用户频道失败'
      }
    }
  }

  async getUserChannel (ctx) {
    const { userId } = ctx.request.query
    const count = await getUserChannel(userId)
    ctx.body = {
      code: 200,
      msg: '获取频道成功',
      data: count
    }
  }
}

module.exports = new ChannelController()